package com.svo.weitu.activity.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.svo.weitu.App;
import com.svo.weitu.R;
import com.svo.weitu.activity.CollectActivity;
import com.svo.weitu.activity.FeedbackActivity;
import com.svo.weitu.activity.SetActivity;
import com.svo.weitu.util.UiUtil;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

public class UserCenterFragment extends Fragment implements OnClickListener{
	private View root;
	private TextView loginTv;
//	private TextView titleTv;
	public static boolean isLoginClick = false;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		root = inflater.inflate(R.layout.user_center, null);
		loginTv = (TextView) root.findViewById(R.id.login_status);
		root.findViewById(R.id.person_login_layout).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				isLoginClick = true;
				login();
			}
		});
		root.findViewById(R.id.my_collect).setOnClickListener(this);
		root.findViewById(R.id.system_set).setOnClickListener(this);
		root.findViewById(R.id.feed_back).setOnClickListener(this);
		root.findViewById(R.id.login_out).setOnClickListener(this);
		root.findViewById(R.id.appwall).setOnClickListener(this);
		return root;
	}
	protected void login() {
		UMShareAPI.get(getActivity()).doOauthVerify(getActivity(), SHARE_MEDIA.QQ, new UMAuthListener() {
			
			@Override
			public void onStart(SHARE_MEDIA arg0) {
				UiUtil.toast(App.context,"请稍候...");
			}
			
			@Override
			public void onError(SHARE_MEDIA arg0, int arg1, Throwable arg2) {
				UiUtil.toast(App.context,"出错了");
			}
			
			@Override
			public void onComplete(SHARE_MEDIA arg0, int arg1, Map<String, String> arg2) {
				UiUtil.toast(App.context,"登录成功");
				Log.w("weitu", "onComplete.....::"+UMShareAPI.get(getActivity()).isAuthorize(getActivity(), SHARE_MEDIA.QQ));
			}
			
			@Override
			public void onCancel(SHARE_MEDIA arg0, int arg1) {
				UiUtil.toast(App.context,"您取消了");
			}
		});		
	}
	@Override
	public void onResume() {
		super.onResume();
		if (getActivity() != null) {
			if (UMShareAPI.get(getActivity()).isAuthorize(getActivity(), SHARE_MEDIA.QQ)) {
				loginTv.setText("已登录");
			}else {
				loginTv.setText("未登录");
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.my_collect:
			startActivity(new Intent(getActivity(), CollectActivity.class));
			getActivity().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
			break;
		case R.id.system_set:
			startActivity(new Intent(getActivity(), SetActivity.class));
			getActivity().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
			break;
		case R.id.feed_back:
			startActivity(new Intent(getActivity(), FeedbackActivity.class));
			break;
		case R.id.login_out:
			UMShareAPI.get(getActivity()).deleteOauth(getActivity(), SHARE_MEDIA.QQ, null);
			Toast.makeText(getActivity(), "已注销", Toast.LENGTH_SHORT).show();
			loginTv.setText("未登录");
			break;
		case R.id.appwall:
			/*int a = new Random().nextInt();
			if (a % 2 == 0) {
			} else {
				AppConnect.getInstance(getActivity()).showAppOffers(getActivity());
			}*/
			break;
		default:
			break;
		}
	}
}
